#!/bin/bash
#centos76 init  + docker-ce ntp 
yum update -y
yum install wget -y
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# aliyun
# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 
# huawei
wget -O /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo
yum clean all
yum makecache
yum install telnet net-tools ntp epel-release container-selinux -y
systemctl enable ntpd
systemctl start ntpd
ntpdate cn.pool.ntp.org
sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2
sudo yum-config-manager \
  --add-repo \
  https://mirrors.huaweicloud.com/docker-ce/linux/centos/docker-ce.repo
sudo yum -y install docker-ce-20.10.3 docker-ce-cli-20.10.3 containerd.io
sudo systemctl enable docker.service
mkdir -p /data/docker/data-root
mkdir -p /etc/docker

cat > /etc/docker/daemon.json <<EOF
{
  "data-root": "/data/docker/data-root",
  "registry-mirrors" : [
    "https://0b4f32bc3d00f3cd0f59c009bb43fea0.mirror.swr.myhuaweicloud.com"
  ]
}
EOF
systemctl enable docker && systemctl restart docker
systemctl restart firewalld
firewall-cmd \
  --add-port=22/tcp \
  --add-port=80/tcp \
  --add-port=443/tcp \
  --permanent
firewall-cmd --reload
cat > /etc/sysctl.d/k8s.conf << EOF
net.ipv4.ip_forward=1
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
vm.swappiness=0
vm.max_map_count=655360 
EOF

sysctl -p /etc/sysctl.d/k8s.conf

curl -L https://get.daocloud.io/docker/compose/releases/download/1.28.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# cat > /usr/local/bin/docker-compose <<EOF
# #!/bin/sh
# #
# # Run docker-compose in a container
# #
# # This script will attempt to mirror the host paths by using volumes for the
# # following paths:
# #   * $(pwd)
# #   * $(dirname $COMPOSE_FILE) if it's set
# #   * $HOME if it's set
# #
# # You can add additional volumes (or any docker run options) using
# # the $COMPOSE_OPTIONS environment variable.
# #
 
 
# set -e
 
# VERSION="1.25.4"
# IMAGE="dy1999/docker-compose:$VERSION"
 
 
# # Setup options for connecting to docker host
# if [ -z "$DOCKER_HOST" ]; then
#     DOCKER_HOST="/var/run/docker.sock"
# fi
# if [ -S "$DOCKER_HOST" ]; then
#     DOCKER_ADDR="-v $DOCKER_HOST:$DOCKER_HOST -e DOCKER_HOST"
# else
#     DOCKER_ADDR="-e DOCKER_HOST -e DOCKER_TLS_VERIFY -e DOCKER_CERT_PATH"
# fi
 
 
# # Setup volume mounts for compose config and context
# if [ "$(pwd)" != '/' ]; then
#     VOLUMES="-v $(pwd):$(pwd)"
# fi
# if [ -n "$COMPOSE_FILE" ]; then
#     COMPOSE_OPTIONS="$COMPOSE_OPTIONS -e COMPOSE_FILE=$COMPOSE_FILE"
#     compose_dir=$(realpath $(dirname $COMPOSE_FILE))
# fi
# # TODO: also check --file argument
# if [ -n "$compose_dir" ]; then
#     VOLUMES="$VOLUMES -v $compose_dir:$compose_dir"
# fi
# if [ -n "$HOME" ]; then
#     VOLUMES="$VOLUMES -v $HOME:$HOME -v $HOME:/root" # mount $HOME in /root to share docker.config
# fi
 
# # Only allocate tty if we detect one
# if [ -t 0 -a -t 1 ]; then
#     DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS -t"
# fi
 
# # Always set -i to support piped and terminal input in run/exec
# DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS -i"
 
 
# # Handle userns security
# if [ ! -z "$(docker info 2>/dev/null | grep userns)" ]; then
#     DOCKER_RUN_OPTIONS="$DOCKER_RUN_OPTIONS --userns=host"
# fi
 
# exec docker run --rm $DOCKER_RUN_OPTIONS $DOCKER_ADDR $COMPOSE_OPTIONS $VOLUMES -w "$(pwd)" $IMAGE "$@"
# EOF
chmod +x /usr/local/bin/docker-compose

echo "end"